﻿using CommonTools;
using CommonTools.Controls;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace shop.cost
{
    /// <summary> 代发指定价格 </summary>
    public class DaifaQtyPostBllUI : DaifaQtyPostBll
    {
        private ControlBuilder _builder;

        public override ControlBuilder GetUIBuilder()
        {
            _builder = base.GetUIBuilder()
                .SetFlowBreak();
                //.AddButton("导入", ImportData, false)
                //.AddButton("下载模板", templateDownload);

            return _builder;
        }

        /// <summary> 导入 </summary>
        public async Task<Result<IEnumerable<dynamic>>> ImportData()
        {
            var dt = ExcelHelper.Import();
            if(dt == null) 
            {
                return new Result<IEnumerable<dynamic>>(false,"没有导入数据");
            }

            //
            var batch = DateTime.Now.ToString("yyyy-MM-ddTHHmmss")+ Guid.NewGuid().ToString();

            var res = await WaitUI.RunAsync(async () =>
            {
                var list = dt.AsEnumerable()
                 .Select(a => new DaifaQtyPostDto()
                 {
                     BeginDate = a.Field<string>(0).IsNullDate(null),
                     EndDate = a.Field<string>(1).IsNullDate(null),
                     Stock = a.Field<string>(2),
                     Province = a.Field<string>(3),
                     spec_no= a.Field<string>(4),
                     RangePriceJson = a.Field<string>(5),
                     //Qty= a.Field<string>(5).IsNull(0),
                     batch= batch,
                 })
                 .Where(a => string.IsNullOrEmpty(a.Stock) == false || string.IsNullOrEmpty(a.Province) == false
                    || string.IsNullOrEmpty(a.spec_no)== false )
                 .ToList();

                var res1 = await base.SaveItem(list);
      
                return res1;
            });

            if (res.IsOk)
            {
                //修改旧的日期  a是旧数据
                //await DB.FSql.Update<DaifaQtyPost>()
                //    .Join<DaifaQtyPost>((a, b) => a.spec_no==b.spec_no && a.Province == b.Province && a.Stock == b.Stock && a.Qty== b.Qty 
                //        && a.id != b.id && b.batch == batch && (a.batch == null || a.batch != batch))
                //    .Set((a, b) => a.EndDate == b.BeginDate.Value.AddDays(-1))
                //    .Where((a, b) => a.isdel == false && b.isdel == false && b.batch == batch && (a.batch == null || a.batch != batch) && a.EndDate >= b.BeginDate)
                //    .ExecuteAffrowsAsync();

                return new Result<IEnumerable<dynamic>>(true, "导入完成");
            }
            else
            {
                return new Result<IEnumerable<dynamic>>(true, res.Message);
                //MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>  </summary>
        public async Task<Result<IEnumerable<dynamic>>> templateDownload()
        {
            var list = (new int[0])
                .Select(a=> new
                {
                    起始日期 = DateTime.Today,
                    截止日期 = DateTime.Today,
                    仓库 = string.Empty,
                    省份 = string.Empty,
                    商家编码 = string.Empty,
                    //数量= 0m,
                    快递费单价 =string.Empty,
                })
                .ToList();

            new ExcelExporter()
                .NewSheet(null)
                .AddList(list)
                .EndThenOpen("成本价格模板");


            return await Task.FromResult(new Result<IEnumerable<dynamic>>(false));
        }


    }
}
